<!DOCTYPE html>
<html>
<head>
<script type="text/javascript"></script>
    <meta charset="UTF-8"/>
    <title>Sample of websocket with golang</title>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

    <script type="text/javascript">
     (function ($) {
                $.getUrlParam = function (name) {
                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                    var r = window.location.search.substr(1).match(reg);
                    if (r != null) return unescape(r[2]); return null;
                }
            })(jQuery);

      $(function() {
        var id = $.getUrlParam("id");
        var ws_url = "ws://localhost:8080/echo?id="+id;
        console.log(ws_url);
        var ws = new WebSocket(ws_url);
        ws.onmessage = function(e) {
          var ee = JSON.parse(e.data);
          console.log(ee);
          //eval(ee["TextMessage"]["Content"]);//通过后端发送的信息来调用前端的数据
          $('<li>').text(e.data).appendTo($ul);
        };
        var $ul = $('#msg-list');
        $('#sendBtn').click(function(){
          var data = $('#name').val();
          ws.send(data);
        });

        function setCookie(c_name,value,expiredays){
          var exdate=new Date()
          exdate.setDate(exdate.getDate()+expiredays)
          document.cookie=c_name+ "=" +escape(value)+
          ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
        }

        setCookie("myctest","testincookie",7)
      });
    </script>
</head>
<body>
<input id="name" type="text"/>
<input type="button" id="sendBtn" value="send"/>
<ul id="msg-list"></ul>
</body>
</html>